//LCD
#include <LiquidCrystal.h>
const int rs = 13, en = 11, d4 = 5, d5 = 7, d6 = 3, d7 = 8;   //改为你选择的引脚
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
String print_data="";
//DHT
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2    //改为你选择的引脚
#define DHTTYPE    DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
//GP2Y10
int measurePin = A5;     //改为你选择的引脚   
int ledPower = 12;         //改为你选择的引脚     
 
unsigned int samplingTime = 280;
unsigned int deltaTime = 40;
unsigned int sleepTime = 9680;
 
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

void setup() {
  Serial.begin(9600);
  //GP2Y10
  pinMode(ledPower,OUTPUT);
  //LCD
  lcd.begin(16, 2);
  lcd.print("Air;Tem;Hum");
  //DHT11
  dht.begin();
/*  Serial.println(F("DHTxx Unified Sensor Example"));*/
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
/*  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  Serial.println(F("------------------------------------"));*/
  dht.humidity().getSensor(&sensor);
 /* Serial.println(F("Humidity Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  Serial.println(F("------------------------------------"));*/
  delayMS = sensor.min_delay / 1000;
/*  Serial.print  (F("delayMS:  ")); Serial.print(delayMS);*/
}

void loop() {
  //air
  digitalWrite(ledPower,LOW);
  delayMicroseconds(samplingTime);
 
  voMeasured = analogRead(measurePin);
 
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH);
  delayMicroseconds(sleepTime);
 
  calcVoltage = voMeasured*(5.0/1024);
  dustDensity = 5000*calcVoltage/29 - 3000/29;
 
  if ( dustDensity < 0)
  {
    dustDensity = 0.00;
  }

/*  Serial.print("Dust Density:");
  Serial.println(dustDensity);*/
  print_data+=dustDensity;
  print_data+=" ";

  //dht11
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
/*    Serial.println(F("Error reading temperature!"));*/
  }
  else {
/*    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));*/
    print_data+=event.temperature;
    print_data+=" ";
  }

  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
 /*   Serial.println(F("Error reading humidity!"));*/
  }
  else {
/*    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));*/
    print_data+=event.relative_humidity;
    print_data+=" ";
  }
  //LCD
   lcd.setCursor(0, 1);
   lcd.print(print_data);
/*   Serial.println(print_data);*/
   print_data="";
   delay(1000);
 
}
